var app = getApp();
var that;
var selectedIndex;
var utils = require('js/utils.js');
var loadIndexData;
Page({
    data:{
        userInfo: {},
        teacherListData:[],
        showModalStatus:false,
        loveModalStatus:false,
        loveText:"您好，要收藏吗？",
        myTeacherList:''
    },
    onLoad:function(){
        that = this;
        app.chooseListItem.isFirst = true;
        utils.loadTeacherList(app.openId,
            function(isok,data){
                if(isok){
                    console.log('load detail ok');
                    that.loadListOk(data);
                }
            }
        );
    },
    onShow:function(){
      console.log('on show');
      if (app.chooseListItem.detailRevise){
        app.chooseListItem.detailRevise = false;
        if (app.chooseListItem.isFirst == false) {
          console.log('on show false');
          loadIndexData.result[app.chooseListItem.userIndex].default = app.chooseListItem.isDefault;
          if (app.chooseListItem.isDefault == 0) {
            var amount = parseInt(loadIndexData.result[app.chooseListItem.userIndex].follow);
            if (amount > 0) {
              amount--;
            }
            loadIndexData.result[app.chooseListItem.userIndex].follow = amount + '';
          } else {
            var amount = parseInt(loadIndexData.result[app.chooseListItem.userIndex].follow);
            amount++;
            loadIndexData.result[app.chooseListItem.userIndex].follow = amount + '';
          }
          that.setData(
            {
              teacherListData: loadIndexData,
              myTeacherList: loadIndexData
            }
          );
        }
      }
    },
    userClick:function(e){
        var index = e.currentTarget.dataset.value;
        console.log('index：'+index);
        var id = that.data.myTeacherList.result[index-1].id;
        app.teacherId = id;
        var item = that.data.myTeacherList.result[index - 1];
        app.chooseListItem.isDefault = item.default;
        app.chooseListItem.userIndex = (index - 1);
        app.chooseListItem.isFirst = false;
        utils.loadTeacherDetail({"tid":id},function(isOk,data){
            if(isOk){
                that.loadTeacherDetailOk(data);
            }
        });
    },
    loveClick:function(e){
        var index = e.currentTarget.dataset.value;
        selectedIndex = index;
        if(that.data.teacherListData[selectedIndex-1].isLove){
            that.setData(
            {
                loveText:"您好，要放弃收藏吗？"
            })
        }else{
            that.setData(
            {
                loveText:"您好，要收藏吗？"
            })
        }
        that.setData(
            {
                loveModalStatus:true
            }
        );
    },
    //加载网络完成
    loadListOk:function(e){
        console.log(e.data);
        loadIndexData = e.data;
        that.setData(
            {
                teacherListData:e.data,
                myTeacherList:e.data
            }
        );
        // console.log(that.data.teacherListData);
    },
    //加载失败
    loadListFail:function(e){
        wx.showToast({
            title: '加载失败',
            icon: 'cancel',
            mask:true,
            duration: 2000
        });
    },
    //加载老师页面完成
    loadTeacherDetailOk(e){
        console.log(e.data.result);
        app.teacherDetailData = e.data.result;
        wx.navigateTo({
          url: '../teacherDetail/teacherDetail',
          success: function(res){
            // success
            console.log('success');
          },
          fail: function() {
            // fail
            console.log('fail');
          },
          complete: function() {
          }
        })
    },
    loadTeacherDetailFail(e){
        wx.showToast({
            title: '加载失败',
            icon: 'cancel',
            mask:true,
            duration: 2000
        });
    },
    modalBindaconfirm:function(){
        that.setData({
            loveModalStatus:false
        })
        console.log(selectedIndex-1);
        that.data.teacherListData[selectedIndex-1].isLove = !that.data.teacherListData[selectedIndex-1].isLove;
        that.setData({
            teacherListData:that.data.teacherListData
        });
    },
    modalBindcancel:function(){
        that.setData({
            loveModalStatus:false
        })
    },
    moreClick:function(e){
        that.setData(
            {
                showModalStatus:true
            })
        console.log("more click");
    },
    hideModal: function () {
        // 隐藏遮罩层
        var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
        })
        this.animation = animation
        animation.translateY(300).step()
        this.setData({
        animationData: animation.export(),
        })
        setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
            animationData: animation.export(),
            showModalStatus: false
        })
        }.bind(this), 200)
    },
    onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '欢迎使用教带',
      path: 'pages/splash/splash',
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})